package pri.zxx.excelgenerate.dataentity.tabledata.aminocapp;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import pri.zxx.excelgenerate.dataentity.entity.CreatRequest;
import pri.zxx.excelgenerate.dataentity.entity.ModuleInfoEnum;
import pri.zxx.excelgenerate.dataentity.entity.ModuleMetaInfo;
import pri.zxx.excelgenerate.dataentity.tabledata.MultiTitleModuleCreatorTemplate;
import pri.zxx.excelgenerate.dataentity.tabledata.TableDefinition;
import pri.zxx.excelgenerate.excelhanler.ExcelUtil;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * [氨基酸模式名称]氨基酸、钙、磷需要量
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Component
public class AminocappModuleCreator extends MultiTitleModuleCreatorTemplate {


    //todo 注入数据获取的bean

    @Override
    public List<ModuleMetaInfo> creator(CreatRequest creatRequest) {

        CreatRequest.TableCreateReq tcr = new CreatRequest.TableCreateReq(1.0, 4, Arrays.asList("日粮需要量水平（%）", "日需要量（g/天）", "与Lys比值（%）", "当前日粮含量（%）"));


        List<ModuleMetaInfo> modules = new ArrayList<>();

        List<AminoCaPp> unLoadData = new ArrayList<>();

        boolean loadRange = true;

        //todo 调用获取数据的service
        switch (creatRequest.getPredictionTypeEnum()) {
            case STARTING_PIGS:
                AminoCaPp st = new AminoCaPp();
                unLoadData.add(st);
                tcr.setLoadRanges(false);
                loadRange = false;
                break;
            case GESTATING_SOWS:
            case LACTATING_SOWS:
            case GROWING_FINISHING:
                AminoCaPp op = new AminoCaPp();
                AminoCaPp op2 = new AminoCaPp();
                AminoCaPp op3 = new AminoCaPp();
                AminoCaPp op4 = new AminoCaPp();
                op.setInitialBwforaa(BigDecimal.valueOf(1)).setFinalBwforaa(BigDecimal.valueOf(30)).setInitialDayinput(1).setFinalDayinput(30);
                op2.setInitialBwforaa(BigDecimal.valueOf(30)).setFinalBwforaa(BigDecimal.valueOf(50)).setInitialDayinput(30).setFinalDayinput(50);
                op3.setInitialBwforaa(BigDecimal.valueOf(50)).setFinalBwforaa(BigDecimal.valueOf(60)).setInitialDayinput(50).setFinalDayinput(60);
                op4.setInitialBwforaa(BigDecimal.valueOf(60)).setFinalBwforaa(BigDecimal.valueOf(80)).setInitialDayinput(60).setFinalDayinput(80);
                unLoadData.add(op);
                unLoadData.add(op2);
                unLoadData.add(op3);
                break;
            default:
                throw new RuntimeException("动物类型异常");
        }
        creatRequest.setTableCreateReq(tcr);

        int tableCount = 1;
        List<CreatRequest.Range> ranges = null;
        if (loadRange) {
            // 有范围要求的表格 根据选择的范围拆分 向上取整
            ranges = creatRequest.getRanges();
            tableCount = (int) Math.ceil(ranges.size() / tcr.getMAX_REPEAT());//创建module数量
        }
        for (int j = 0; j < tableCount; j++) {

            ModuleMetaInfo metaInfo = new ModuleMetaInfo();
            TableDefinition tableDefinition = new TableDefinition();

            Boolean isWeight = ExcelUtil.isWeight(creatRequest);
            CreatRequest cr = new CreatRequest();

            if (loadRange) {
                List<CreatRequest.Range> rg = ranges.stream().skip((long) tcr.getMAX_REPEAT() * j).limit((long) tcr.getMAX_REPEAT()).collect(Collectors.toList());
                List<AminoCaPp> pageData = unLoadData.stream().filter(opVitamin -> {
                    //判断是体重范围 还是 时间范围
                    String rangeStr;
                    if (isWeight) {
                        BigDecimal finalBwforaa = opVitamin.getFinalBwforaa();
                        BigDecimal initialBwforaa = opVitamin.getInitialBwforaa();
                        rangeStr = initialBwforaa.toString() + "-" + finalBwforaa.toString();
                    } else {
                        Integer initialDayinput = opVitamin.getInitialDayinput();
                        Integer finalDayinput = opVitamin.getFinalDayinput();
                        rangeStr = initialDayinput + "-" + finalDayinput;
                    }
                    return rg.stream().anyMatch(range -> rangeStr.equals(range.getStart() + "-" + range.getEnd()));
                }).collect(Collectors.toList());
                BeanUtils.copyProperties(creatRequest, cr);
                cr.setRanges(rg);
                tableDefinition.setUnLoadData(pageData).setDataClass(AminoCaPp.class);
            } else {
                cr = creatRequest;
                tableDefinition.setUnLoadData(unLoadData).setDataClass(AminoCaPp.class);
            }

            //组装数据
            tableDefinition = dataHandle(moduleMetaInfoHandle(tableDefinition, cr), cr);
            metaInfo.setTableDefinition(tableDefinition);
            String s = "";
            if (tableCount > 1) {
                s = JOIN_DECILITER + (j + 1);
            }
            metaInfo.setModuleTitle(moduleInfo().getName() + s).setModuleInfoEnum(moduleInfo());
            modules.add(metaInfo);
        }
        return modules;
    }

    @Override
    public ModuleInfoEnum moduleInfo() {
        return ModuleInfoEnum.AMINOCAPP;
    }

}
